<?php

namespace backend\models\form;

use backend\models\AdminUser;
use common\models\Evalu;
use common\models\Feed;
use common\service\ConstatService;
use Yii;
use yii\base\Model;

class EvaluForm extends Model
{
    public $title;
    public $desc1;
    public $desc2;
    public $desc3;
    public $desc4;
    public $desc5;
    public $desc6;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title'], 'required'],
            [['desc1', 'desc2','desc3','desc4','desc5','desc6','title'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'title' => '标题 ',
            'desc1' => '描述1',
            'desc2' => '描述2',
            'desc3' => '描述3',
            'desc4' => '描述4',
            'desc5' => '描述5',
            'desc6' => '描述6',
        ];
    }

    /**
     * 新增
     * @return array
     */
    public function add(){
        $model = new Evalu();

        $curTime = date("Y-m-d H:i:s") ;

        $content = "" ;
        if(!empty($this->desc1)){
            $content .= $this->desc1 ."|";
        }

        if(!empty($this->desc2)){
            $content .= $this->desc2 ."|";
        }

        if(!empty($this->desc3)){
            $content .= $this->desc3 ."|";
        }

        if(!empty($this->desc4)){
            $content .= $this->desc4 ."|";
        }

        if(!empty($this->desc5)){
            $content .= $this->desc5 ."|";
        }

        if(!empty($this->desc6)){
            $content .= $this->desc6 ."|";
        }

        if(empty($content)){
            return ["code"=>400,"msg"=>"描述不能为空"];
        }

        $content = mb_substr($content,0,-1) ;


        $model->title = $this->title ;
        $model->content = $content ;
        $model->status = ConstatService::EVALU_STATUS_ENABLE ;
        $model->upd_time = $curTime ;

        $save = $model->save();
        if($save){
            return ["code"=>200,"msg"=>"成功"];
        }else{
            $err = current($model->getFirstErrors()) ;
            return ["code"=>400,"msg"=>"添加失败-".$err];
        }
    }

    /**
     * 编辑
     * @return array
     */
    public function edit($id){

        $model = Evalu::findOne(["id"=>$id]) ;

        if(empty($model)){
            return ["code"=>400,"msg"=>"记录不存在"];
        }

        $curTime = date("Y-m-d H:i:s") ;

        $content = "" ;
        if(!empty($this->desc1)){
            $content .= $this->desc1 ."|";
        }

        if(!empty($this->desc2)){
            $content .= $this->desc2 ."|";
        }

        if(!empty($this->desc3)){
            $content .= $this->desc3 ."|";
        }

        if(!empty($this->desc4)){
            $content .= $this->desc4 ."|";
        }

        if(!empty($this->desc5)){
            $content .= $this->desc5 ."|";
        }

        if(!empty($this->desc6)){
            $content .= $this->desc6 ."|";
        }

        if(empty($content)){
            return ["code"=>400,"msg"=>"描述不能为空"];
        }

        $content = mb_substr($content,0,-1) ;

        $model->title = $this->title ;
        $model->content = $content ;
        $model->upd_time = $curTime ;

        $save = $model->save();
        if($save){
            return ["code"=>200,"msg"=>"成功"];
        }else{
            $err = current($model->getFirstErrors()) ;
            return ["code"=>400,"msg"=>"修改失败-".$err];
        }
    }
}
